<template>
    <div class="container ">
        <Breadcrumb :categoryInfo="categoryInfo" v-if="!$U.isEmptyObj(categoryInfo)" />
        <div class="sub-container">
            <el-tabs v-model="reqData.sortField" @tab-change="tabChange">
                <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
                <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
                <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
            </el-tabs>
            <div class="body" v-infinite-scroll="load" :infinite-scroll-disabled="disabled">
                <!-- 商品列表-->
                <Good v-for="good in subCategoryGoods" :good="good" :key="good.id" />
            </div>
        </div>
    </div>
</template>

<script setup>
import Breadcrumb from './components/breadcrumb.vue';
import Good from './components/good.vue'

import { storeToRefs } from 'pinia';
import { useSubCategoryStore } from '@/stores/subCategory';

const subCategoryStore = useSubCategoryStore();
const { categoryInfo, subCategoryGoods, disabled, isLoading } = storeToRefs(subCategoryStore);

// 获取参数
const route = useRoute()
const router = useRouter()

const { proxy } = getCurrentInstance();

const $U = proxy.$U

// 请求参数
const reqData = ref({
    categoryId: route.params.id,
    page: 1,
    pageSize: 20,
    sortField: 'publishTime'
})

watch(() => route.params.id,
    (newVal, oldVal) => {
        // 二级路由
        if (router.currentRoute.value.fullPath.split('/').length === 4) {
            subCategoryStore.getCategory(newVal);
            subCategoryStore.getSubCategoryData(reqData.value);
        }
    },
    { immediate: true }
)

// tab切换回调
function tabChange() {
    // console.log('tab切换了', reqData.value.sortField)
    reqData.value.page = 1
    subCategoryStore.changeData({
        disabled: false,
        list: [],
    });
    subCategoryStore.getSubCategoryData(reqData.value);
}

// 加载更多
const load = async () => {
    // console.log('加载更多数据咯')
    if (isLoading.value || disabled.value) return
    // 获取下一页的数据
    reqData.value.page++
    subCategoryStore.getSubCategoryData(reqData.value);
}
</script>

<style lang="scss" scoped>
.sub-container {
    padding: 20px 10px;
    background-color: #fff;

    .body {
        display: flex;
        flex-wrap: wrap;
        padding: 0 10px;
    }

    .goods-item {
        display: block;
        width: 220px;
        margin-right: 20px;
        padding: 20px 30px;
        text-align: center;

        img {
            width: 160px;
            height: 160px;
        }

        p {
            padding-top: 10px;
        }

        .name {
            font-size: 16px;
        }

        .desc {
            color: #999;
            height: 29px;
        }

        .price {
            color: $priceColor;
            font-size: 20px;
        }
    }

    .pagination-container {
        margin-top: 20px;
        display: flex;
        justify-content: center;
    }


}
</style>